import { login, getInfo } from '@/api/login'
import { message } from "ant-design-vue";
import { fetchData } from "@/utils/common";

import router, { resetRouter } from '@/router'

const state = {
  token: localStorage.getItem('token') ? localStorage.getItem('token') : '', // 认证凭证'
  userName: '',
  roles: [],
  introduce: ''
}
const mutations = {
  SET_TOKEN(state, val) {
    state.token = val
    localStorage.setItem('token', val)
  },
  DEL_TOKEN(state) {
    state.token = ''
    state.userName = ''
    state.roles = ''
    state.introduce = ''
    localStorage.removeItem('token')
  },
  SET_ROLES(state, payload) {
    state.roles = payload
  },
  SET_NAME(state, payload) {
    state.userName = payload
  },
  SET_INTRODUCE(state, payload) {
    state.introduce = payload
  }
}
const actions = {
  // user login
  _login({ commit }, formdatas) {
  
     
    return new Promise((resolve, reject) => {

        fetchData(login,formdatas).then((data)=>{
       
            message.success(data.msg)
                      commit('SET_TOKEN', data.token)
            resolve(data)

          })

        
    //   login(formdatas)
    //     .then(res => {
    //       if (res.code === 0) {
    //         if (res.data.success) {
    //             message.success(res.data.msg)
    //           commit('SET_TOKEN', res.data.token)
    //         } else {
    //             message.error(res.data.msg)
    //         }
    //         resolve(res)
    //       }
    //     })
    //     .catch(error => {
    //       reject(error)
    //     })
    })
  },
  loginOut({ commit }) {
    commit('DEL_TOKEN')
    resetRouter()
    router.push({
      path: '/login',
      query: {
        redirect: '/'
      }
    })
  },
  _getInfo({ commit }) {

   
    return new Promise((resolve, reject) => {



        fetchData(getInfo).then((data)=>{
            const { name, roles, introduce } = data
                commit('SET_ROLES', roles)
                commit('SET_NAME', name)
                commit('SET_INTRODUCE', introduce)
                console.log(data,999999999999);
                resolve(data)
        })







    //   getInfo()
    //     .then(res => {
    //       if (res.code === 0) {
    //         const { name, roles, introduce } = res.data
    //         commit('SET_ROLES', roles)
    //         commit('SET_NAME', name)
    //         commit('SET_INTRODUCE', introduce)
    //       } else {
    //         message.error(res.msg)
    //       }
    //       resolve(res.data)
    //     })
    //     .catch(error => {
    //       reject(error)
    //     })
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
